import { _decorator, Component, Node, Sprite, clamp01 } from 'cc';
import { BasicComponet } from '../../Init/Basic/BasicComponet';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
const { ccclass, property } = _decorator;

@ccclass('BuffCd')
export class BuffCd extends BasicComponet {
    @property(Sprite)
    sprite: Sprite = null;
    @property(Node)
    icos: Node = null;

    _type: GlobalEnum.BuffType;

    setData(type) {
        this._type = type;
        // this.sprite = this.node.getComponent(Sprite);
        if (this.sprite) {
            this.sprite.fillRange = 1;
        }
        for (let i = 0; i < this.icos.children.length; i++) {
            const e = this.icos.children[i];
            e.active = i == (+type);
        }
    }

    reset() {
        if (this.sprite) {
            this.sprite.fillRange = 1;
        }
        this.node.active = false;
    }

    customUpdate(dt) {
        if (!GlobalTmpData.BuffRecs[this._type]) return;
        const rec = GlobalTmpData.BuffRecs[this._type];
        let r = rec.curt / rec.delay;
        this.sprite.fillRange = clamp01(1 - r);

        this.node.active = rec.curt > 0 && rec.curt != rec.delay;
    }

}

